package com.orangelife.mobile.goodsrent.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.curry.android.util.DecimalUtil;
import com.orangelife.mobile.common.db.DBHelper;
import com.orangelife.mobile.common.db.DBOperate;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/11.
 */
public class GoodsDao {
    public static final String ID = "_id";
    public static final String GOODS_ID = "goodsId";
    public static final String GOODS_NAME = "goodsName";
    public static final String GOODS_DISPATCH_TYPE = "goodsDelivery";
    public static final String GOODS_RENT_DAY = "rentDay";
    public static final String GOODS_UNIT_PRIDE = "unitPrice";
    public static final String GOODS_SIGLE_PRICE = "price";//单天的价格
    public static final String GOODS_RENT_MAX_DAY = "rentMaxDay";
    public static final String GOODS_MER_ID = "merID";
    public static final String GOODS_LOGO = "goodsLogo";
    private DBOperate dbOperate;
    private SQLiteDatabase sqLiteDatabase;
    private static GoodsDao goodsDao;

    public static GoodsDao getInstance() {
        if (goodsDao == null) {
            goodsDao = new GoodsDao();
        }
        return goodsDao;
    }

    public GoodsDao() {
        dbOperate = DBOperate.getInstance(DBHelper.getInstance());
    }

    //插入数据
    public Long insertUserInfo(GoodsInfo goodsInfo) {
        sqLiteDatabase = dbOperate.getWritableDatabase();
        Long uid = null;
        try {
            ContentValues values = new ContentValues();
            values.put(GOODS_ID, goodsInfo.getrGoodID());
            values.put(GOODS_NAME, goodsInfo.getGoodName());
            values.put(GOODS_RENT_DAY, goodsInfo.getDay());
            values.put(GOODS_SIGLE_PRICE, goodsInfo.getPrice());
            values.put(GOODS_UNIT_PRIDE, goodsInfo.getUnitPrice());
            values.put(GOODS_DISPATCH_TYPE, goodsInfo.getDispatchType());
            values.put(GOODS_RENT_MAX_DAY, goodsInfo.getPdtDesc());
            values.put(GOODS_MER_ID, goodsInfo.getMerID());
            values.put(GOODS_LOGO, goodsInfo.getGoodsLogo());
            uid = sqLiteDatabase.insert(DBHelper.TB_GOODS_RENT, ID, values);
        } catch (Exception e) {
        } finally {
            dbOperate.closeDatabase();
        }
        return uid;
    }

    public boolean isExistGood(String goodsID) {
        if (goodsID == null) {
            return false;
        }
        sqLiteDatabase = DBHelper.getInstance().getReadableDatabase();
        boolean isExist = false;
        Cursor cursor = null;
        try {
            cursor = sqLiteDatabase.query(DBHelper.TB_GOODS_RENT, null, GOODS_ID + "=?", new String[]{goodsID}, null, null, null);
            isExist = cursor.moveToFirst();
        } catch (Exception e) {
        } finally {
            cursor.close();
            dbOperate.closeDatabase();
        }
        return isExist;
    }

    //更新天数和总价
    public int setRentDay(GoodsInfo goodsInfo, String day, String price) {
        sqLiteDatabase = dbOperate.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(GOODS_RENT_DAY, day);
        values.put(GOODS_UNIT_PRIDE, price);
        int id = sqLiteDatabase.update(DBHelper.TB_GOODS_RENT, values, GOODS_ID + "=?", new String[]{goodsInfo.getrGoodID()});
        return id;
    }

    //删除数据
    public int deleteGoodsInfo(GoodsInfo goodsInfo) {
        sqLiteDatabase = dbOperate.getWritableDatabase();
        int id = 0;
        try {
            id = sqLiteDatabase.delete(DBHelper.TB_GOODS_RENT, GOODS_ID + "=?", new String[]{goodsInfo.getrGoodID()});
        } catch (Exception e) {
        } finally {
            dbOperate.closeDatabase();
        }
        return id;
    }

    //清空购物车
    public int deleteAllGoodsInfo() {
        sqLiteDatabase = dbOperate.getWritableDatabase();
        int id = 0;
        try {
            id = sqLiteDatabase.delete(DBHelper.TB_GOODS_RENT, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbOperate.closeDatabase();
        }
        return id;
    }

    //查询物品数量
    public int getGoodsNum() {
        sqLiteDatabase = dbOperate.getReadableDatabase();
        int num = 0;
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select count(*) from " + DBHelper.TB_GOODS_RENT, null);
            cursor.moveToFirst();
            num = cursor.getInt(0);
        } catch (SQLiteException e) {
            // FIXME: 2016/4/18 这里存在一个可能发生数据表不存在的问题，但并不知道表为什么没有在Application中
            e.printStackTrace();
        }
        return num;
    }

    //计算物品总价(根据每个物品的价格的合计)
    public String getGoodsTotalPrice() {
        sqLiteDatabase = dbOperate.getReadableDatabase();
        Cursor cursor = null;
        List<String> list = null;
        String price = "0";
        try {
            list = new ArrayList<>();
            cursor = sqLiteDatabase.rawQuery("select " + GOODS_UNIT_PRIDE + " from " + DBHelper.TB_GOODS_RENT, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                list.add(cursor.getString(0));
                cursor.moveToNext();
            }
            for (int i = 0; i < list.size(); i++) {
                price = DecimalUtil.add(price, list.get(i));
            }
        } catch (Exception e) {
        } finally {
            cursor.close();
            dbOperate.closeDatabase();
        }
        return price;
    }

    //查询物品列表
    public List<GoodsInfo> selectUserInfoList() {
        sqLiteDatabase = dbOperate.getReadableDatabase();
        List<GoodsInfo> goodsInfos = null;
        Cursor cursor = null;
        try {
            goodsInfos = new ArrayList<>();
            cursor = sqLiteDatabase.rawQuery("select * from " + DBHelper.TB_GOODS_RENT, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast() && cursor.getString(1) != null) {
                GoodsInfo goodsInfo = new GoodsInfo();
                goodsInfo.setrGoodID(cursor.getString(1));
                goodsInfo.setGoodName(cursor.getString(2));
                goodsInfo.setDay(cursor.getString(3));
                goodsInfo.setPrice(cursor.getString(4));
                goodsInfo.setUnitPrice(cursor.getString(5));
                goodsInfo.setDispatchType(cursor.getString(6));
                goodsInfo.setPdtDesc(cursor.getString(7));
                goodsInfo.setMerID(cursor.getString(8));
                goodsInfo.setGoodsLogo(cursor.getString(9));
                goodsInfos.add(goodsInfo);
                cursor.moveToNext();
            }
        } catch (Exception e) {
        } finally {
            cursor.close();
            dbOperate.closeDatabase();
        }
        return goodsInfos;
    }

}
